﻿using UnityEngine;
using UnityEngine.Rendering;
using UnityEngine.UI;
using URP.WarFog;

namespace Learning
{
    public class SimpleWarFogArea : MonoBehaviour
    {
        public InputField inputField;
        
        public GameObject originImage;
        
        public GameObject warFogStatic;
        public GameObject warFogStatic2;
        
        public Texture2D area;
        
        private RTHandle maskTex1, maskTex2;
        
        public Material blitMat;

        public float step;

        public Vector4[] uvsData;
        
        void OnEnable()
        {
            var uvs = Resources.Load<WarFogUV>("WarFogUV");
            uvsData = uvs.uv;
            
            var width = area.width;
            var height = area.height;
            var format = area.graphicsFormat;
            maskTex1 = RTHandles.Alloc(width, height, colorFormat: format, name: "BlitTexName");
            maskTex2 = RTHandles.Alloc(width, height, colorFormat: format, name: "BlitTexName2");
            Graphics.Blit(area, maskTex1);
            Shader.SetGlobalTexture("_FogMaskTex", maskTex1);

            {
                var warMat = originImage.GetComponent<MeshRenderer>().sharedMaterial;
                warMat.SetTexture("_MainTex", maskTex1);
            }
            
            {
                var warMat = warFogStatic.GetComponent<MeshRenderer>().sharedMaterial;
                warMat.SetTexture("_MainTex", maskTex1);
            }
            
            {
                var warMat = warFogStatic2.GetComponent<MeshRenderer>().sharedMaterial;
                warMat.SetTexture("_MainTex", maskTex1);
            }
        }

        public void Open()
        {
            var areaId = int.Parse(inputField.text);

            var uv = uvsData[areaId];
            if (uv.y < 2)
            {
                uv.y += 2;
            }
            uvsData[areaId] = uv;
        }

        public void Close()
        {
            var areaId = int.Parse(inputField.text);

            var uv = uvsData[areaId];
            if (uv.y >= 2)
            {
                uv.y -= 2;
            }
            uvsData[areaId] = uv;
        }

        void Update()
        {
            Render();
        }

        public void Render()
        {
            var buffer = CommandBufferPool.Get("WarFogBlit");
            
            buffer.SetGlobalFloat("_Step", step);
            buffer.SetGlobalVectorArray("uvs", uvsData);
            
            buffer.Blit(maskTex1, maskTex2, blitMat);
            buffer.Blit(maskTex2, maskTex1, blitMat);
            
            // 结果存储到全局贴图 _FogMaskTex 上，可以再别的需要使用的地方直接在 Shader 中使用
            buffer.SetGlobalTexture("_FogMaskTex", maskTex1);
            Shader.SetGlobalTexture("_FogMaskTex", maskTex1);
            
            Graphics.ExecuteCommandBuffer(buffer);
            
            CommandBufferPool.Release(buffer);
        }

        void OnDisable()
        {
            RTHandles.Release(maskTex1);
            RTHandles.Release(maskTex2);
        }
    }
}
